﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Microsoft.VisualBasic;

namespace sdd.subsystems.config
{
    class FileConfigurator : IConfigurator
    {
        public Director Director { set; get; }

        public bool LoadConfigurator(string fileName)
        {
            try
            {
                // load configurator code
                string line = "";
                using (StreamReader sr = new StreamReader(fileName))
                {
                    while (!sr.EndOfStream)
                    {
                        line = sr.ReadLine();

                    }
                    sr.Close();
                }

                // all is good
                return true;
            }
            catch
            {
                // summit went wrong
                return false;
            }
        }

        private bool EvaluateCfgLine(string line)
        {
            if (!line.StartsWith("#"))
            {
                string command = GetCommand(line).ToLower();
                string arguments = GetArguments(line);
                switch (command)
                {
                    case "license":
                        return EvaluateCmdLicense(arguments);
                    case "echo":
                        Console.WriteLine(arguments);
                        return true;
                    default:
                        Console.WriteLine("UNKNOWN CONFIG COMMAND: " + line);
                        return false;
                }
            }
            else
            {
                // ignore comments
                return true;
            }
        }

        private bool EvaluateCmdLicense(string arguments)
        {
            
            return true;
        }

        private string GetCommand(string input)
        {
            int sepPos = Strings.InStr(input, " ");
            if (sepPos == 0)
            {
                return input;
            }
            else
            {
                return Strings.Left(input, sepPos - 1);
            }
        }
        

        private string GetArguments(string input)
        {
            int sepPos = Strings.InStr(input, " ");
            if (sepPos == 0)
            {
                return "";
            }
            else
            {
                return Strings.Mid(input, sepPos + 1);
            }
        }

    }
}
